package com.enation.app.shop.front.api.inpolicy.service.impl;

import com.enation.app.shop.front.api.inpolicy.entity.InPolicy;
import com.enation.app.shop.front.api.inpolicy.service.IInpolicyManager;
import com.enation.framework.database.IDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("warrantyservice")
public class WarrantyManager implements IInpolicyManager {

    @Autowired
    private IDaoSupport daoSupport;


    @Override
    public List<InPolicy> getWarrantyByuserid(Integer userid) {
        String sql="select * from rbt_inpolicy where user_id= ? ";
//        List<InPolicy> warranties=this.daoSupport.queryForList(sql,InPolicy.class,userid);
//        return warranties;
        return  this.daoSupport.queryForList(sql,InPolicy.class,userid);
    }

    @Override
    public List<InPolicy> getWarrantyByuseridAndStatus(Integer userid,Integer i) {
        String sql="select * from rbt_inpolicy where user_id= ?  and status=?   and is_policy=1";
//        List<InPolicy> warranties=this.daoSupport.queryForList(sql,InPolicy.class,userid);
//        return warranties;
        return  this.daoSupport.queryForList(sql,InPolicy.class,userid,i);
    }

    @Override
    public InPolicy getWarrantyByuseridAndRecentTime(Integer userid){
        String sql="SELECT * from rbt_inpolicy WHERE warranty_id in" +
                "(SELECT MAX(warranty_id) FROM rbt_inpolicy WHERE user_id=?)";
        InPolicy warrantie=daoSupport.queryForObject(sql,InPolicy.class,userid);
        return warrantie;
    };

    @Override
    public int createWarranty(InPolicy warranty) {
        this.daoSupport.insert("rbt_inpolicy",warranty);
//        int warrantyid = this.daoSupport.getLastId("rbt_warranty");
//        if (warrantyid!=0){
//            return   warrantyid;
//        }else {
//            return  0;
//        }
        return  1;
    }

    @Override
    public InPolicy warranty(String warrantynumber) {
        String sql="select * from rbt_inpolicy where warranty_number =?";
        return this.daoSupport.queryForObject(sql, InPolicy.class, warrantynumber);
    }

    @Override
    public InPolicy getById(Integer id) {
        String sql="select * from rbt_inpolicy where warranty_id =?";
        return this.daoSupport.queryForObject(sql, InPolicy.class, id);
    }

    @Override
    public int editwarranty(InPolicy warranty) {
        this.daoSupport.update("rbt_inpolicy",warranty,"warranty_id="+warranty.getWarranty_id());
        return 1;
    }


}
